package com.zbcz.erp.activity

import android.os.Bundle
import android.util.Log
import androidx.activity.viewModels
import com.zbcz.erp.adapter.MyApplyHistoryAdapter
import com.zbcz.erp.databinding.ActivityMyApplyHistoryBinding
import com.zbcz.erp.util.DensityUtils
import com.zbcz.erp.util.onSingleClick
import com.zbcz.erp.viewmodel.MyApplyHistoryViewModel

class MyApplyHistoryActivity : BaseActivity() {

    companion object {
        const val TAG = "MyApplyHistoryActivity"
    }

    private val mViewModel: MyApplyHistoryViewModel by viewModels()

    private lateinit var mBinding: ActivityMyApplyHistoryBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        mBinding = ActivityMyApplyHistoryBinding.inflate(layoutInflater)
        setContentView(mBinding.root)
        super.onCreate(savedInstanceState)
        init()
    }

    fun init() {
        mBinding.clParent.setPadding(
            0,
            DensityUtils.dp2px(this, 48f),
            0,
            0
        )
        mBinding.clTitle.ivCommonBack.onSingleClick {
            finish()
        }
        mBinding.clTitle.tvTitle.text = "审批历史"
        val id = intent?.getStringExtra("id")
        mViewModel.getHistoryFlow(id ?: "")
        mViewModel.mHistoryLiveData.observe(this) {
            Log.i(TAG, it.toString())
            val mAdapter = MyApplyHistoryAdapter()
            mBinding.rcList.adapter = mAdapter
            mAdapter.submitList(it)
            mBinding.refreshLayout.finishRefresh()
        }
        mBinding.refreshLayout.setEnableRefresh(true)
        mBinding.refreshLayout.setEnableLoadMore(false)
        mBinding.refreshLayout.setOnRefreshListener {
            mViewModel.getHistoryFlow(id ?: "")
        }
    }
}